// pages/user-hyzx-hyxx/user-hyzx-hyxx.js
var util = require('../../utils/network.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
      username:'',
      usernickname:'',
      realname:'',
      idnum:'',
      sex:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var userInfo=JSON.parse(wx.getStorageSync('userInfo'))
    var that=this
    that.setData({
        username: userInfo.username,
        usernickname: userInfo.usernickname,
        realname: userInfo.realname,
        idnum: userInfo.idnum,
    })
    if (userInfo.sex==1){
        that.setData({
            sex:'女',
        })
    }else{
        that.setData({
            sex: '男',
        })
    }
  },
  //提交
  formSubmit: function (e) {
      var token = util.token
      var that = this
      console.log(e)
      var gender = e.detail.value.gender
      var sex
      if (gender=='女'){
          sex=1
      }else{
          sex=2
      }
      var idcard = e.detail.value.idcard
      var name = e.detail.value.name
      var nickname = e.detail.value.nickname
      var username = e.detail.value.username
      if (gender != '' && idcard != '' && name != '' && nickname != '' && username!='') {
          wx.request({
              url: util.main + '/user/updateuser',
              data: {
                  "user.id": wx.getStorageSync('userId'),
                  "user.username": username,
                  "user.usernickname": nickname,
                  "user.realname": name,
                  "user.idnum": idcard,
                  "user.sex": sex,
                  "deviceid": wx.getStorageSync('openId'),
                  "token": token
              },
              header: {
                  "content-type": "application/x-www-form-urlencoded",
                  "accept": "application/json,",
              },
              method: "POST",
              dataType: "json",
              success: function (res) {
                  if (res.statusCode == 200) {
                      wx.redirectTo({
                          url: '../user-hyzx/user-hyzx',
                      })
                  }
                  console.log(res)
              }
          })
      }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})